package service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import serviceInterface.HelperServiceInterface;
import util.DateUtil;
import util.IConstant;
import domain.Driver;
import domain.Envelope;
import domain.EnvelopeType;
import domain.LogisticManager;
import domain.Mission;
import domain.MissionStatus;
import domain.POS;
import domain.Person;
import domain.Route;
import domain.Schedule;
import domain.ScheduleStatus;
import domain.TopManager;

@Stateless
public class HelperService implements HelperServiceInterface{

	@PersistenceContext
	private EntityManager entityManager;
	
	public void createTestSchedule1() throws ParseException{
		// ADD Person
		Person person = new Person();
			person.setFullName("Anucha");
			person.setUserName("driver1");
			person.setPassword("driver1");
			try {
				person.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		Driver driver = new Driver();
			person.addRoles(driver);
		
		entityManager.persist(person);
		entityManager.flush();
		
		
		
		Person person2 = new Person();
			person2.setFullName("Suriyan");
			person2.setUserName("driver2");
			person2.setPassword("driver2");
			try {
				person2.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			Driver driver2 = new Driver();
			person2.addRoles(driver2);
	
			entityManager.persist(person2);
			entityManager.flush();
	
		Person person1 = new Person();
			person1.setFullName("Paul");
			person1.setUserName("manager");
			person1.setPassword("manager");
			try {
				person1.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		LogisticManager logistic = new LogisticManager();
			person1.addRoles(logistic);
		
		entityManager.persist(person1);
		entityManager.flush();
		
		
		// ADD POS
		POS pos777 = new POS();
			pos777.setName(IConstant.WARE_HOUSE);
			pos777.setLatitute(13.711428);
			pos777.setLongtitute(100.594103);
			pos777.setPosId("777");
			pos777.setCentralGroup(false);
		entityManager.persist(pos777);
			
		// ROUTE 12
		POS pos336 = new POS();
			pos336.setName("The Mall Bangkae");
			pos336.setLatitute(13.714556);
			pos336.setLongtitute(100.407947);
			pos336.setPosId("336");
			pos336.setCentralGroup(false);
		entityManager.persist(pos336);
			
		POS pos331 = new POS();
			pos331.setName("Tung-Hau-Seng Thonburi");
			pos331.setLatitute(13.787295);
			pos331.setLongtitute(100.486796);
			pos331.setPosId("331");
			pos331.setCentralGroup(false);
		entityManager.persist(pos331);
			
		POS pos360 = new POS();
			pos360.setName("Tung-Hua-Seng Banglumpu");
			pos360.setLatitute(13.761176);
			pos360.setLongtitute(100.497574);
			pos360.setPosId("360");
			pos360.setCentralGroup(false);
		entityManager.persist(pos360);
			
		POS pos302 = new POS();
			pos302.setName("Central RAMA II");
			pos302.setLatitute(13.662638);
			pos302.setLongtitute(100.44299);
			pos302.setPosId("302");
			pos302.setCentralGroup(true);
		entityManager.persist(pos302);
			
		POS posS38 = new POS();
			posS38.setName("Booth Market Rama II");
			posS38.setLatitute(13.662638);
			posS38.setLongtitute(100.44299);
			posS38.setPosId("S38");
			posS38.setCentralGroup(false);
		entityManager.persist(posS38);
		
		POS pos118 = new POS();
			pos118.setName("GL Central Pinklao");
			pos118.setLatitute(13.777779);
			pos118.setLongtitute(100.476534);
			pos118.setPosId("118");
			pos118.setCentralGroup(true);
		entityManager.persist(pos118);
	
		//Others
		POS pos349 = new POS();
			pos349.setName("Mall Emporium");
			pos349.setLatitute(13.730008);
			pos349.setLongtitute(100.56972);
			pos349.setPosId("349");
			pos349.setCentralGroup(false);
		entityManager.persist(pos349);
		
		POS pos121 = new POS();
			pos121.setName("GL Central Ladprao");
			pos121.setLatitute(13.816398);
			pos121.setLongtitute(100.561079);
			pos121.setPosId("121");
			pos121.setCentralGroup(true);
		entityManager.persist(pos121);
		
		POS pos165 = new POS();
			pos165.setName("Fashion Islands");
			pos165.setLatitute(13.829746);
			pos165.setLongtitute(100.672064);
			pos165.setPosId("165");
			pos165.setCentralGroup(false);
		entityManager.persist(pos165);
		
		POS pos185 = new POS();
			pos185.setName("GL Amarin Plaza");
			pos185.setLatitute(13.74375);
			pos185.setLongtitute(100.541261);
			pos185.setPosId("185");
			pos185.setCentralGroup(false);
		entityManager.persist(pos185);
		
		POS pos151 = new POS();
			pos151.setName("Jousse Central Ladprao");
			pos151.setLatitute(13.816398);
			pos151.setLongtitute(100.561079);
			pos151.setPosId("151");
			pos151.setCentralGroup(true);
		entityManager.persist(pos151);
		
		POS pos116 = new POS();
			pos116.setName("Central Rama 3");
			pos116.setLatitute(13.697492);
			pos116.setLongtitute(100.538097);
			pos116.setPosId("116");
			pos116.setCentralGroup(true);
		entityManager.persist(pos116);
		
		POS pos135 = new POS();
			pos135.setName("Louis Dusit Hotel");
			pos135.setLatitute(13.728788);
			pos135.setLongtitute(100.538863);
			pos135.setPosId("135");
			pos135.setCentralGroup(false);
		entityManager.persist(pos135);
		
		POS pos129 = new POS();
			pos129.setName("GR Central Silom Complex");
			pos129.setLatitute(13.728587);
			pos129.setLongtitute(100.534865);
			pos129.setPosId("129");
			pos129.setCentralGroup(true);
		entityManager.persist(pos129);
		
		POS pos334 = new POS();
			pos334.setName("The Mall Tapla");
			pos334.setLatitute(13.713285);
			pos334.setLongtitute(100.479042);
			pos334.setPosId("334");
			pos334.setCentralGroup(true);
		entityManager.persist(pos334);
		
		POS posP21 = new POS();
//			posP21.setName("The Mall Tapla");
//			posP21.setLatitute(13.713285);
//			posP21.setLongtitute(100.479042);
			posP21.setPosId("P21");
//			posP21.setCentralGroup(true);
		entityManager.persist(posP21);
			
		entityManager.flush();	
		
		//Add schedule
		Schedule schedule = new Schedule();
		schedule.setRoute(Route.R12);
		schedule.setDate(DateUtil.getCurrentTimestamp());
		schedule.setStatus(ScheduleStatus.unpriortized);
		
		Envelope e3000000000069 = new Envelope();
			e3000000000069.setEnvelopId("3000000000069");
			e3000000000069.setEnvelopeType(EnvelopeType.pickUp);
			e3000000000069.setProductName(IConstant.PRODUCT_F);
			e3000000000069.setWeight(0);
		entityManager.persist(e3000000000069);
		
		Envelope e3000000000144 = new Envelope();
			e3000000000144.setEnvelopId("3000000000144");
			e3000000000144.setEnvelopeType(EnvelopeType.pickUp);
			e3000000000144.setProductName(IConstant.PRODUCT_F);
			e3000000000144.setWeight(3);
		entityManager.persist(e3000000000144);
		
		Envelope e3000000000151 = new Envelope();
			e3000000000151.setEnvelopId("3000000000151");
			e3000000000151.setEnvelopeType(EnvelopeType.pickUp);
			e3000000000151.setProductName(IConstant.PRODUCT_G);
			e3000000000151.setWeight(0);
		entityManager.persist(e3000000000151);
		
		Envelope e3000000000168 = new Envelope();
			e3000000000168.setEnvelopId("3000000000168");
			e3000000000168.setEnvelopeType(EnvelopeType.transfer);
			e3000000000168.setProductName(IConstant.PRODUCT_G);
			e3000000000168.setWeight(0);
			e3000000000168.setTransferFrom(pos360);
			e3000000000168.setTransferTo(pos302);
		entityManager.persist(e3000000000168);
		
//		Envelope e3000000000175 = new Envelope();
//			e3000000000175.setEnvelopId("3000000000175");
//			e3000000000175.setEnvelopeType(EnvelopeType.transfer);
//			e3000000000175.setProductName(IConstant.PRODUCT_G);
//			e3000000000175.setWeight(0);
//			e3000000000175.setTransferFrom(pos360);
//			e3000000000175.setTransferTo(posS38);
//		entityManager.persist(e3000000000175);
		
		Envelope e3000000000182 = new Envelope();
			e3000000000182.setEnvelopId("3000000000182");
			e3000000000182.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000182.setProductName(IConstant.PRODUCT_G);
			e3000000000182.setWeight(2);
		entityManager.persist(e3000000000182);
		
		Envelope e3000000000199 = new Envelope();
			e3000000000199.setEnvelopId("3000000000199");
			e3000000000199.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000199.setProductName(IConstant.PRODUCT_G);
			e3000000000199.setWeight(0);
		entityManager.persist(e3000000000199);
		
		Envelope e3000000000205 = new Envelope();
			e3000000000205.setEnvelopId("3000000000205");
			e3000000000205.setEnvelopeType(EnvelopeType.pickUp);
			e3000000000205.setProductName(IConstant.PRODUCT_J);
			e3000000000205.setWeight(0);
		entityManager.persist(e3000000000205);
		
		Envelope e3000000000212 = new Envelope();
			e3000000000212.setEnvelopId("3000000000212");
			e3000000000212.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000212.setProductName(IConstant.PRODUCT_J);
			e3000000000212.setWeight(1);
		entityManager.persist(e3000000000212);
		
		Envelope e3000000000229 = new Envelope();
			e3000000000229.setEnvelopId("3000000000229");
			e3000000000229.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000229.setProductName(IConstant.PRODUCT_L);
			e3000000000229.setWeight(1);
		entityManager.persist(e3000000000229);
		
		Envelope e3000000000236 = new Envelope();
			e3000000000236.setEnvelopId("3000000000236");
			e3000000000236.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000236.setProductName(IConstant.PRODUCT_P);
			e3000000000236.setWeight(1);
		entityManager.persist(e3000000000236);
		
		Envelope e3000000000243 = new Envelope();
			e3000000000243.setEnvelopId("3000000000243");
			e3000000000243.setEnvelopeType(EnvelopeType.dropOff);
			e3000000000243.setProductName(IConstant.PRODUCT_P);
			e3000000000243.setWeight(0);
		entityManager.persist(e3000000000243);
		
		Envelope e3000000000274 = new Envelope();
			e3000000000274.setEnvelopId("3000000000274");
			e3000000000274.setEnvelopeType(EnvelopeType.refill);
			e3000000000274.setProductName(IConstant.PRODUCT_F);
			e3000000000274.setWeight(0);
		entityManager.persist(e3000000000274);
		
		Envelope e3000000000281 = new Envelope();
			e3000000000281.setEnvelopId("3000000000281");
			e3000000000281.setEnvelopeType(EnvelopeType.refill);
			e3000000000281.setProductName(IConstant.PRODUCT_F);
			e3000000000281.setWeight(0);
		entityManager.persist(e3000000000281);
		
		Envelope e3000000000304 = new Envelope();
			e3000000000304.setEnvelopId("3000000000304");
			e3000000000304.setEnvelopeType(EnvelopeType.refill);
			e3000000000304.setProductName(IConstant.PRODUCT_G);
			e3000000000304.setWeight(0);
		entityManager.persist(e3000000000304);
		
		Envelope e3000000000311 = new Envelope();
			e3000000000311.setEnvelopId("3000000000311");
			e3000000000311.setEnvelopeType(EnvelopeType.refill);
			e3000000000311.setProductName(IConstant.PRODUCT_J);
			e3000000000311.setWeight(0);
		entityManager.persist(e3000000000311);
		
		Envelope e3000000000328 = new Envelope();
			e3000000000328.setEnvelopId("3000000000328");
			e3000000000328.setEnvelopeType(EnvelopeType.refill);
			e3000000000328.setProductName(IConstant.PRODUCT_J);
			e3000000000328.setWeight(0);
		entityManager.persist(e3000000000328);
		
		Envelope e3000000000342 = new Envelope();
			e3000000000342.setEnvelopId("3000000000342");
			e3000000000342.setEnvelopeType(EnvelopeType.refill);
			e3000000000342.setProductName(IConstant.PRODUCT_G);
			e3000000000342.setWeight(0);
		entityManager.persist(e3000000000342);
		
		Envelope e3000000000359 = new Envelope();
			e3000000000359.setEnvelopId("3000000000359");
			e3000000000359.setEnvelopeType(EnvelopeType.refill);
			e3000000000359.setProductName(IConstant.PRODUCT_S);
			e3000000000359.setWeight(0);
		entityManager.persist(e3000000000359);
	
		entityManager.flush();
		
		Mission mission336 = new Mission();
			mission336.setToPOS(pos336);
			mission336.setStatus(MissionStatus.incomplete);
			mission336.addEnvelopes(e3000000000069);
			mission336.addEnvelopes(e3000000000243);
			mission336.addEnvelopes(e3000000000281);
			mission336.addEnvelopes(e3000000000328);
		schedule.addMission(mission336);
			
		Mission mission331 = new Mission();
			mission331.setToPOS(pos331);
			mission331.setStatus(MissionStatus.incomplete);
			mission331.addEnvelopes(e3000000000144);
			mission331.addEnvelopes(e3000000000151);
			mission331.addEnvelopes(e3000000000205);
		schedule.addMission(mission331);
		
		Mission mission360 = new Mission();
			mission360.setToPOS(pos360);
			mission360.setStatus(MissionStatus.incomplete);
			mission360.addEnvelopes(e3000000000168);
//			mission360.addEnvelopes(e3000000000175);
		schedule.addMission(mission360);
		
		Mission mission302 = new Mission();
			mission302.setToPOS(pos302);
			mission302.setStatus(MissionStatus.incomplete);
			mission302.addEnvelopes(e3000000000168);
			mission302.addEnvelopes(e3000000000212);
			mission302.addEnvelopes(e3000000000311);
		schedule.addMission(mission302);
		
		Mission missionS38 = new Mission();
			missionS38.setToPOS(posS38);
			missionS38.setStatus(MissionStatus.incomplete);
//			missionS38.addEnvelopes(e3000000000175);
			missionS38.addEnvelopes(e3000000000199);
			missionS38.addEnvelopes(e3000000000229);
			missionS38.addEnvelopes(e3000000000236);
			missionS38.addEnvelopes(e3000000000359);
		schedule.addMission(missionS38);
		
		Mission mission118 = new Mission();
			mission118.setToPOS(pos118);
			mission118.setStatus(MissionStatus.incomplete);
			mission118.addEnvelopes(e3000000000182);
			mission118.addEnvelopes(e3000000000274);
			mission118.addEnvelopes(e3000000000304);
			mission118.addEnvelopes(e3000000000342);
		schedule.addMission(mission118);
		
		logistic.addSchedules(schedule);
		
		entityManager.persist(schedule);
		entityManager.flush();
		
		
		//another schedule
		Schedule tschedule = new Schedule();
		tschedule.setRoute(Route.R12);
		tschedule.setDate(DateUtil.getCurrentTimestamp());
		tschedule.setStatus(ScheduleStatus.priortized);
		
		POS tpos2 = new POS();
			tpos2.setName("Central Pinklao");
			tpos2.setLatitute(13.777779);
			tpos2.setLongtitute(100.476534);
			tpos2.setCentralGroup(true);
			
		POS tpos3 = new POS();
			tpos3.setName("Tung-Hau-Seng Thonburi");
			tpos3.setLatitute(13.787295);
			tpos3.setLongtitute(100.486796);
			tpos3.setCentralGroup(false);
			
		POS tpos4 = new POS();
			tpos4.setName("Tung-Hua-Seng Banglumpu");
			tpos4.setLatitute(13.761176);
			tpos4.setLongtitute(100.497574);
			tpos4.setCentralGroup(false);
		
		POS tpos5 = new POS();
			tpos5.setName("The Mall Bangkae");
			tpos5.setLatitute(13.714556);
			tpos5.setLongtitute(100.407947);
			tpos5.setCentralGroup(false);
			
		entityManager.persist(tpos2);
		entityManager.persist(tpos3);
		entityManager.persist(tpos4);
		entityManager.persist(tpos5);
		entityManager.flush();
		
		Envelope te1 = new Envelope();
			te1.setEnvelopeType(EnvelopeType.refill);
			te1.setEnvelopId("3000000000991");
			te1.setNoOfProduct(10);
		Envelope te2 = new Envelope();
			te2.setEnvelopeType(EnvelopeType.refill);
			te2.setEnvelopId("3000000000992");
			te2.setNoOfProduct(5);
		Envelope te4 = new Envelope();
			te4.setEnvelopeType(EnvelopeType.refill);
			te4.setEnvelopId("3000000000993");
			te4.setNoOfProduct(6);
		
		entityManager.persist(te1);
		entityManager.persist(te2);
		entityManager.persist(te4);
		entityManager.flush();
		
		Mission tmission = new Mission();
			tmission.setFromPOS(pos777);
			tmission.setToPOS(tpos2);
			tmission.setPriority(1);
			tmission.setStatus(MissionStatus.incomplete);
			tmission.addEnvelopes(te1);
			
		Mission tmission2 = new Mission();
			tmission2.setFromPOS(tpos2);
			tmission2.setToPOS(tpos3);
			tmission2.setPriority(2);
			tmission2.setStatus(MissionStatus.incomplete);
			tmission2.addEnvelopes(te2);
		  
		Mission tmission3 = new Mission();
			tmission3.setFromPOS(tpos3);
			tmission3.setToPOS(tpos4);
			tmission3.setPriority(3);
			tmission3.setStatus(MissionStatus.incomplete);
			
		Mission tmission4 = new Mission();
			tmission4.setFromPOS(tpos3);
			tmission4.setToPOS(tpos5);
			tmission4.setPriority(4);
			tmission4.setStatus(MissionStatus.incomplete);
			tmission4.addEnvelopes(te4);
		
		tschedule.addMission(tmission);
		tschedule.addMission(tmission2);
		tschedule.addMission(tmission3);
		tschedule.addMission(tmission4);

		driver.addSchedules(tschedule);

		entityManager.persist(tschedule);
		entityManager.flush();
		
		
		// ********** Mocking up report data *********** //
		Person person5 = new Person();
			person5.setFullName("Teerachai");
			person5.setUserName("driver3");
			person5.setPassword("driver3");
			try {
				person5.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		Driver driver3 = new Driver();
			person5.addRoles(driver3);
			
		entityManager.persist(person5);
		entityManager.flush();
			
		//another schedule
		Schedule schedule3 = new Schedule();
		schedule3.setRoute(Route.R22);
		schedule3.setDate(DateUtil.getTimestampFromDDMMYYYY("20/04/2011", 0));
		schedule3.setStatus(ScheduleStatus.completed);
		
		POS pos155 = new POS();
			pos155.setName("GR Central Ladprao");
			pos155.setLatitute(13.777779);
			pos155.setLongtitute(100.476534);
			pos155.setCentralGroup(true);
			
		POS pos166 = new POS();
			pos166.setName("Louis Emporium");
			pos166.setLatitute(13.730008);
			pos166.setLongtitute(100.56972);
			pos166.setCentralGroup(false);
			
		POS pos127 = new POS();
			pos127.setName("GR Emporium");
			pos127.setLatitute(13.730008);
			pos127.setLongtitute(100.56972);
			pos127.setCentralGroup(false);
		
		POS pos125 = new POS();
			pos125.setName("Time Square");
//			pos125.setLatitute(13.714556);
//			pos125.setLongtitute(100.407947);
			pos125.setCentralGroup(false);
			
		entityManager.persist(pos155);
		entityManager.persist(pos166);
		entityManager.persist(pos127);
		entityManager.persist(pos125);
		entityManager.flush();
		
		Envelope e31 = new Envelope();
			e31.setEnvelopeType(EnvelopeType.pickUp);
			e31.setEnvelopId("3000000001001");
			e31.setNoOfProduct(10);
		Envelope e32 = new Envelope();
			e32.setEnvelopeType(EnvelopeType.dropOff);
			e32.setEnvelopId("3000000001002");
			e32.setNoOfProduct(5);
		Envelope e33 = new Envelope();
			e33.setEnvelopeType(EnvelopeType.refill);
			e33.setEnvelopId("3000000001003");
			e33.setNoOfProduct(6);
		Envelope e34 = new Envelope();
			e34.setEnvelopeType(EnvelopeType.newProduct);
			e34.setEnvelopId("3000000001004");
			e34.setNoOfProduct(2);
		Envelope e35 = new Envelope();
			e35.setEnvelopeType(EnvelopeType.refill);
			e35.setEnvelopId("3000000001005");
			e35.setNoOfProduct(5);
		Envelope e36 = new Envelope();
			e36.setEnvelopeType(EnvelopeType.refill);
			e36.setEnvelopId("3000000001006");
			e36.setNoOfProduct(2);
		
		entityManager.persist(e31);
		entityManager.persist(e32);
		entityManager.persist(e33);
		entityManager.persist(e34);
		entityManager.persist(e35);
		entityManager.persist(e36);
		entityManager.flush();
		
		Mission m31 = new Mission();
			m31.setFromPOS(pos777);
			m31.setToPOS(pos125);
			m31.setPriority(1);
			m31.setStatus(MissionStatus.complete);
			m31.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:40:00", 0));
			m31.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:00:00", 0));
			m31.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:45:00", 0));
			m31.addEnvelopes(e31);
			
		Mission m32 = new Mission();
			m32.setFromPOS(pos125);
			m32.setToPOS(pos127);
			m32.setPriority(2);
			m32.setStatus(MissionStatus.complete);
			m32.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:33:00", 0));
			m32.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:45:00", 0));
			m32.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:35:00", 0));
			m32.addEnvelopes(e32);
		  
		Mission m33 = new Mission();
			m33.setFromPOS(pos127);
			m33.setToPOS(pos349);
			m33.setPriority(3);
			m33.setStatus(MissionStatus.complete);
			m33.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:05:00", 0));
			m33.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:35:00", 0));
			m33.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m33.addEnvelopes(e33);
			
		Mission m34 = new Mission();
			m34.setFromPOS(pos349);
			m34.setToPOS(pos166);
			m34.setPriority(4);
			m34.setStatus(MissionStatus.complete);
			m34.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:25:00", 0));
			m34.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m34.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m34.addEnvelopes(e34);
		
		Mission m35 = new Mission();
			m35.setFromPOS(pos166);
			m35.setToPOS(pos151);
			m35.setPriority(5);
			m35.setStatus(MissionStatus.complete);
			m35.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:21:00", 0));
			m35.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m35.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
			m35.addEnvelopes(e35);
		
		Mission m36 = new Mission();
			m36.setFromPOS(pos166);
			m36.setToPOS(pos151);
			m36.setPriority(6);
			m36.setStatus(MissionStatus.complete);
			m36.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:30:00", 0));
			m36.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
			m36.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:40:00", 0));
			m36.addEnvelopes(e36);
		
		Mission m37 = new Mission();
			m37.setFromPOS(pos151);
			m37.setToPOS(pos121);
			m37.setPriority(7);
			m37.setStatus(MissionStatus.complete);
			m37.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:45:00", 0));
			m37.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:40:00", 0));
			m37.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:17:00:00", 0));
		
		schedule3.addMission(m31);
		schedule3.addMission(m32);
		schedule3.addMission(m33);
		schedule3.addMission(m34);
		schedule3.addMission(m35);
		schedule3.addMission(m36);
		schedule3.addMission(m37);

		driver3.addSchedules(schedule3);

		entityManager.persist(schedule3);
		entityManager.flush();
			
		Person person6 = new Person();
			person6.setFullName("Manit");
			person6.setUserName("driver4");
			person6.setPassword("driver4");
			try {
				person6.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		Driver driver4 = new Driver();
			person6.addRoles(driver4);
		
		entityManager.persist(person6);
		entityManager.flush();
		
		//another schedule
		Schedule schedule4 = new Schedule();
		schedule4.setRoute(Route.R32);
		schedule4.setDate(DateUtil.getTimestampFromDDMMYYYY("20/04/2011", 0));
		schedule4.setStatus(ScheduleStatus.completed);
		
		POS pos332 = new POS();
			pos332.setName("The Mall Ramkhamhaeng");
			pos332.setCentralGroup(false);
			
		POS pos311 = new POS();
			pos311.setName("CT Bangna");
			pos311.setCentralGroup(true);
			
		POS pos114 = new POS();
			pos114.setName("Lotus Bangkabi");
			pos114.setCentralGroup(false);
		
		POS posP12 = new POS();
			posP12.setName("Seri Center");
			posP12.setCentralGroup(false);
			
		entityManager.persist(pos332);
		entityManager.persist(pos311);
		entityManager.persist(pos114);
		entityManager.persist(posP12);
		entityManager.flush();
		
		Envelope e41 = new Envelope();
			e41.setEnvelopeType(EnvelopeType.pickUp);
			e41.setEnvelopId("3000000001011");
			e41.setNoOfProduct(3);
		Envelope e42 = new Envelope();
			e42.setEnvelopeType(EnvelopeType.dropOff);
			e42.setEnvelopId("3000000001012");
			e42.setNoOfProduct(5);
		Envelope e43 = new Envelope();
			e43.setEnvelopeType(EnvelopeType.refill);
			e43.setEnvelopId("3000000001013");
			e43.setNoOfProduct(6);
		Envelope e44 = new Envelope();
			e44.setEnvelopeType(EnvelopeType.newProduct);
			e44.setEnvelopId("3000000001014");
			e44.setNoOfProduct(9);
		
		entityManager.persist(e41);
		entityManager.persist(e42);
		entityManager.persist(e43);
		entityManager.persist(e44);
		entityManager.flush();
		
		Mission m41 = new Mission();
			m41.setFromPOS(pos777);
			m41.setToPOS(pos332);
			m41.setPriority(1);
			m41.setStatus(MissionStatus.complete);
			m41.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:40:00", 0));
			m41.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:00:00", 0));
			m41.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m41.addEnvelopes(e41);
			
		Mission m42 = new Mission();
			m42.setFromPOS(pos332);
			m42.setToPOS(pos114);
			m42.setPriority(2);
			m42.setStatus(MissionStatus.complete);
			m42.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:33:00", 0));
			m42.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m42.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m42.addEnvelopes(e42);
		  
		Mission m43 = new Mission();
			m43.setFromPOS(pos114);
			m43.setToPOS(posP12);
			m43.setPriority(3);
			m43.setStatus(MissionStatus.complete);
			m43.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:18:00", 0));
			m43.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m43.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:20:00", 0));
			m43.addEnvelopes(e43);
			
		Mission m44 = new Mission();
			m44.setFromPOS(posP12);
			m44.setToPOS(pos166);
			m44.setPriority(4);
			m44.setStatus(MissionStatus.complete);
			m44.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:25:00", 0));
			m44.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m44.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m44.addEnvelopes(e34);
		
		Mission m45 = new Mission();
			m45.setFromPOS(pos166);
			m45.setToPOS(pos311);
			m45.setPriority(5);
			m45.setStatus(MissionStatus.complete);
			m45.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:19:00", 0));
			m45.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m45.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
	
		schedule4.addMission(m41);
		schedule4.addMission(m42);
		schedule4.addMission(m43);
		schedule4.addMission(m44);
		schedule4.addMission(m45);

		driver4.addSchedules(schedule4);

		entityManager.persist(schedule4);
		entityManager.flush();
		
		//another schedule
		Schedule schedule5 = new Schedule();
		schedule5.setRoute(Route.R32);
		schedule5.setDate(DateUtil.getTimestampFromDDMMYYYY("21/04/2011", 0));
		schedule5.setStatus(ScheduleStatus.completed);
		
		Mission m51 = new Mission();
			m51.setFromPOS(pos777);
			m51.setToPOS(pos332);
			m51.setPriority(1);
			m51.setStatus(MissionStatus.complete);
			m51.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m51.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:00:00", 0));
			m51.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m51.addEnvelopes(e41);
			
		Mission m52 = new Mission();
			m52.setFromPOS(pos332);
			m52.setToPOS(pos114);
			m52.setPriority(2);
			m52.setStatus(MissionStatus.complete);
			m52.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:50:00", 0));
			m52.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m52.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m52.addEnvelopes(e42);
		  
		Mission m53 = new Mission();
			m53.setFromPOS(pos114);
			m53.setToPOS(posP12);
			m53.setPriority(3);
			m53.setStatus(MissionStatus.complete);
			m53.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:40:00", 0));
			m53.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m53.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:20:00", 0));
			m53.addEnvelopes(e43);
			
		Mission m54 = new Mission();
			m54.setFromPOS(posP12);
			m54.setToPOS(pos166);
			m54.setPriority(4);
			m54.setStatus(MissionStatus.complete);
			m54.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:25:00", 0));
			m54.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m54.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m44.addEnvelopes(e34);
		
		Mission m55 = new Mission();
			m55.setFromPOS(pos166);
			m55.setToPOS(pos311);
			m55.setPriority(5);
			m55.setStatus(MissionStatus.complete);
			m55.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:19:00", 0));
			m55.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m55.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
	
		schedule5.addMission(m51);
		schedule5.addMission(m52);
		schedule5.addMission(m53);
		schedule5.addMission(m54);
		schedule5.addMission(m55);

		driver4.addSchedules(schedule5);

		entityManager.persist(schedule5);
		entityManager.flush();
		
		//another schedule
		Schedule schedule6 = new Schedule();
		schedule6.setRoute(Route.R32);
		schedule6.setDate(DateUtil.getTimestampFromDDMMYYYY("22/04/2011", 0));
		schedule6.setStatus(ScheduleStatus.completed);
		
		Mission m61 = new Mission();
			m61.setFromPOS(pos777);
			m61.setToPOS(pos332);
			m61.setPriority(1);
			m61.setStatus(MissionStatus.complete);
			m61.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m61.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:00:00", 0));
			m61.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m61.addEnvelopes(e41);
			
		Mission m62 = new Mission();
			m62.setFromPOS(pos332);
			m62.setToPOS(pos114);
			m62.setPriority(2);
			m62.setStatus(MissionStatus.complete);
			m62.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:50:00", 0));
			m62.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m62.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m62.addEnvelopes(e42);
		  
		Mission m63 = new Mission();
			m63.setFromPOS(pos114);
			m63.setToPOS(posP12);
			m63.setPriority(3);
			m63.setStatus(MissionStatus.complete);
			m63.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:40:00", 0));
			m63.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m63.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:20:00", 0));
			m63.addEnvelopes(e43);
			
		Mission m64 = new Mission();
			m64.setFromPOS(posP12);
			m64.setToPOS(pos166);
			m64.setPriority(4);
			m64.setStatus(MissionStatus.complete);
			m64.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:31:00", 0));
			m64.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m64.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m64.addEnvelopes(e34);
		
		Mission m65 = new Mission();
			m65.setFromPOS(pos166);
			m65.setToPOS(pos311);
			m65.setPriority(5);
			m65.setStatus(MissionStatus.complete);
			m65.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:12:00", 0));
			m65.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m65.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
	
		schedule6.addMission(m61);
		schedule6.addMission(m62);
		schedule6.addMission(m63);
		schedule6.addMission(m64);
		schedule6.addMission(m65);

		driver4.addSchedules(schedule6);
		
		entityManager.persist(schedule6);
		entityManager.flush();
		
		//another schedule
		Schedule schedule7 = new Schedule();
		schedule7.setRoute(Route.R32);
		schedule7.setDate(DateUtil.getTimestampFromDDMMYYYY("23/04/2011", 0));
		schedule7.setStatus(ScheduleStatus.completed);
		
		Mission m71 = new Mission();
			m71.setFromPOS(pos777);
			m71.setToPOS(pos332);
			m71.setPriority(1);
			m71.setStatus(MissionStatus.complete);
			m71.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m71.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:12:00:00", 0));
			m71.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m71.addEnvelopes(e41);
			
		Mission m72 = new Mission();
			m72.setFromPOS(pos332);
			m72.setToPOS(pos114);
			m72.setPriority(2);
			m72.setStatus(MissionStatus.complete);
			m72.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:50:00", 0));
			m72.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:13:30:00", 0));
			m72.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m72.addEnvelopes(e42);
		  
		Mission m73 = new Mission();
			m73.setFromPOS(pos114);
			m73.setToPOS(posP12);
			m73.setPriority(3);
			m73.setStatus(MissionStatus.complete);
			m73.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:20:00", 0));
			m73.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:35:00", 0));
			m73.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:15:20:00", 0));
			m73.addEnvelopes(e43);
			
		Mission m74 = new Mission();
			m74.setFromPOS(posP12);
			m74.setToPOS(pos166);
			m74.setPriority(4);
			m74.setStatus(MissionStatus.complete);
			m74.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:31:00", 0));
			m74.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:00:00", 0));
			m74.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m74.addEnvelopes(e34);
		
		Mission m75 = new Mission();
			m75.setFromPOS(pos166);
			m75.setToPOS(pos311);
			m75.setPriority(5);
			m75.setStatus(MissionStatus.complete);
			m75.setCompleteTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:12:00", 0));
			m75.setStartTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:14:30:00", 0));
			m75.setEndTime(DateUtil.getTimestampFromDDMMYYYYHHMMSS("20/04/2011:16:20:00", 0));
	
		schedule7.addMission(m71);
		schedule7.addMission(m72);
		schedule7.addMission(m73);
		schedule7.addMission(m74);
		schedule7.addMission(m75);

		driver4.addSchedules(schedule7);

		entityManager.persist(schedule7);
		entityManager.flush();
		
		Person person4 = new Person();
			person4.setFullName("P. Sukhum");
			person4.setUserName("top");
			person4.setPassword("top");
			try {
				person4.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		TopManager topManager1 = new TopManager();
//			topManager1.addDrivers(driver);
//			topManager1.addDrivers(driver2);
//			topManager1.addDrivers(driver3);
//			topManager1.addDrivers(driver4);
			person4.addRoles(topManager1);
		
		entityManager.persist(person4);
		entityManager.flush();
	}
}